package com.study.demo;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Objects;


/**
 * @author zhuzi
 * 2023/9/9 20:41
 */
@Data
@Accessors(chain = true)
public class QsBean {

    private String bondId;
    private String bondNm;

    /**
     * 三低 | 价格*(1+溢价率)*规模
     */
    private BigDecimal sd;
    private BigDecimal price;
    /**
     * 溢价率
     */
    private BigDecimal yjl;
    private String stockId;
    private String stockNm;
    private String marginFlg;
    /**
     * 类型 C 转债  | E 可交债
     */
    private String btype;
    private String origIssAmt;
    /**
     * 剩余规模
     */
    private BigDecimal currIssAmt;
    private LocalDate convertDt;
    /**
     * 转股价
     */
    private BigDecimal convertPrice;
    private String nextPutDt;
    private LocalDate redeemDt;
    private String forceRedeem;
    /**
     * Y 强赎 N 不强赎 X 天数
     */
    private String redeemFlag;
    private String redeemPrice;
    private String redeemPriceRatio;
    private String realForceRedeemPrice;
    /**
     * 强赎还剩几天(-1是已经达到强赎)|没有达到计时天数的也是按照-1计算的,因此还要判断redeemFlag
     */
    private int redeemRemainDays;
    private int redeemRealDays;
    private int redeemTotalDays;
    private LocalDate recountDt;
    private int redeemCountDays;
    private String redeemTc;
    /**
     * 正股价
     */
    private BigDecimal sprice;
    /**
     * 到期日
     */
    private LocalDate maturityDt;

    private String upperPriceAdj;
    private String redeemIcon;
    private int redeemOrders;
    private int atMaturity;
    private String redeemCount;
    private int afterNextPutDt;
    private String forceRedeemPrice;


    /**
     * 计算溢价率和三低
     */
    public void calc() {
        calcYjl();
        calcSd();
    }

    public void calcYjl() {
        if (Objects.nonNull(sprice) && Objects.nonNull(convertPrice) && Objects.nonNull(price)) {
            //溢价率 = 100*(转债价格/(100*正股价/转股价)-1)
            BigDecimal mul = NumberUtil.mul(100, sprice);
            BigDecimal div = NumberUtil.div(mul, convertPrice);
            BigDecimal sub = NumberUtil.sub(NumberUtil.div(price, div), 1);
            BigDecimal yjl1 = NumberUtil.mul(100, sub);
            this.yjl = yjl1.setScale(2, BigDecimal.ROUND_HALF_UP);
        }
    }

    /**
     * 过滤
     * <p>
     * 排除沪市转债，强赎天数小于5，公告强赎债，ST债|剩余年限半年以上
     * </p>
     *
     * @return
     */
    public boolean filter() {
        //System.out.println(StrUtil.format("{}{}{}{}|{}", bondId, bondNm, stockNm, redeemFlag, redeemRemainDays));
        boolean type = this.getBtype().equals("C");
        boolean st = !StrUtil.contains(this.getStockNm(), "ST");
        boolean y = (!this.getRedeemFlag().equals("Y")) && (redeemRemainDays < 0 || NumberUtil.compare(this.getRedeemRemainDays(), 5) >= 0);
        boolean bondN = StrUtil.startWith(bondId, "12");
        //剩余时间半年以上
        boolean before = LocalDate.now().isBefore(maturityDt.minusMonths(6));
        return BooleanUtil.and(type, st, y, bondN, before);
    }

    public void calcSd() {
        BigDecimal sdCal = NumberUtil.mul(price, currIssAmt,
                NumberUtil.add(1,
                        NumberUtil.div(yjl, 100)
                )
        );
        this.sd = sdCal.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
